package com.bilu.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bilu.common.bean.ElecData;
import com.bilu.common.utility.ResponseUtility;
import com.bilu.common.utility.TimeUtility;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

public class TableService {
    public static void table(JSONObject json, HttpServletResponse response) {
    	String cause = json.getString("cause");
    	String beginTime = json.getString("beginTime");
    	String endTime = json.getString("endTime");
    	JSONArray elecDataArray = json.getJSONArray("elecDataList");
    	String provider = json.getString("provider");
    	String providerDate = json.getString("providerDate");
    	String scouter = json.getString("scouter");
    	String scouterDate = json.getString("scouterDate");
    	String witness = json.getString("witness");
    	String witnessDate = json.getString("witnessDate");
    	
    	Map<String, Object> data = new HashMap<String, Object>();
    	if(cause != null) {
    		data.put("cause", cause);
    	}
    	
    	if(beginTime != null && beginTime.trim().length() > 0 && endTime != null && endTime.trim().length() > 0) {
    		data.put("fixedTime", TimeUtility.getFormatTime(TimeUtility.getDate(beginTime, "yyyy-MM-dd HH:mm"), "yyyy年MM月dd日HH时mm分") + "至" + TimeUtility.getFormatTime(TimeUtility.getDate(endTime, "yyyy-MM-dd HH:mm"), "yyyy年MM月dd日HH时mm分"));
    	}
    	
    	if(elecDataArray != null && elecDataArray.size() > 0) {
    		List<ElecData> elecDataList = new ArrayList<ElecData>();
    		for(int index = 0;index < elecDataArray.size();index++) {
    			JSONObject elecData = elecDataArray.getJSONObject(index);
    			elecDataList.add(new ElecData(elecData.getString("elecDesc"), elecData.getString("type"), elecData.getString("source"), elecData.getString("fileFormat"), elecData.getString("md5"), elecData.getString("remark")));
    		}
    		data.put("elecDataList", elecDataList);
    	}
    	
    	if(provider != null) {
    		data.put("provider", provider);
    	}
    	
    	if(providerDate != null && providerDate.trim().length() > 0) {
    		data.put("providerDate", TimeUtility.getFormatTime(TimeUtility.getDate(providerDate, "yyyy-MM-dd"), "yyyy年MM月dd日"));
    	}
    	
    	if(scouter != null) {
    		data.put("scouter", scouter);
    	}
    	
    	if(scouterDate != null && scouterDate.trim().length() > 0) {
    		data.put("scouterDate", TimeUtility.getFormatTime(TimeUtility.getDate(scouterDate, "yyyy-MM-dd"), "yyyy年MM月dd日"));
    	}
    	
    	if(witness != null) {
    		data.put("witness", witness);
    	}
    	
    	if(witnessDate != null && witnessDate.trim().length() > 0) {
    		data.put("witnessDate", TimeUtility.getFormatTime(TimeUtility.getDate(witnessDate, "yyyy-MM-dd"), "yyyy年MM月dd日"));
    	}
    	
		//创建行循环策略
		LoopRowTableRenderPolicy rowTableRenderPolicy = new LoopRowTableRenderPolicy();
		//告诉模板引擎，要在employees做行循环,绑定行循环策略
		Configure configure = Configure.builder().bind("elecDataList", rowTableRenderPolicy).build();
		
		ResponseUtility.response(response, "table", System.currentTimeMillis() + ".docx", data, configure);
    }
}
